<template>
  <div>
    <div class="search">
      <p class="float-left mr-10 mb-10" style="width: 200px">
        <el-input v-model="searchParams.name" placeholder="输入名称"></el-input>
      </p>
      <p class="float-left mr-10 mb-10">
        <el-button type="primary" @click="searchLearn">搜索</el-button>
        <el-button type="primary" @click="resetLearn">重置</el-button>
      </p>
    </div>
    <div class="my-10">
      <bdButton :handlerBtn="handlerBtn"/>
    </div>
    <bdTable :tableHeader="tableHeader" :tableData="tableData" class="mb-10" />
    <bd-page
      :pageSize="pages"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></bd-page>
    <addLearnAndEditLearn ref="learnInfo" @update:visible="searchLearn(true)"/>
    <el-dialog
      :visible.sync="videoDialogVisible"
      append-to-body
      class="video-dialog"
      :show-close="false"
      destroy-on-close
    >
      <video width="100%" controls="controls" :src="fileUrl | getImgUrl"></video>
    </el-dialog>
  </div>
</template>
<script>
import { eLearnkPage, deleteELearn } from '@/api/elearn'
import addLearnAndEditLearn from './addLearnAndEditLearn'
import { getImgUrl } from '@/filters/filters.js'
export default {
  name: '',
  components: {
    addLearnAndEditLearn
  },
  data () {
    return {
      searchParams: {
        name: null,
        type: 'redVideo'
      },
      videoDialogVisible: false,
      fileUrl: '',
      handlerBtn: [
        {
          id: 1,
          name: '新增',
          handler: this.addLearn
        }
      ],
      tableHeader: {
        type: 'selection',
        header: [
          {
            prop: 'name',
            label: '标题'
          },
          {
            prop: 'fmUrl',
            type: 'img',
            label: '视频封面',
            previewSrcList: (row) => {
              return [getImgUrl(row.fmUrl)]
            }
          },
          {
            prop: 'fileUrl',
            type: 'video',
            label: '视频预览',
            handlerPreview: (row) => {
              if (!row.fileUrl) return
              this.fileUrl = row.fileUrl
              this.videoDialogVisible = true
            }
          },
          {
            prop: 'createTime',
            label: '创建日期'
          },
          {
            label: '操作',
            type: 'button',
            minWidth: 150,
            btn: [
              {
                id: 1,
                name: '编辑',
                handler: this.handleEdit
              },
              {
                id: 2,
                name: '删除',
                type: 'danger',
                handler: this.handleDelete
              }
            ]
          }
        ]
      },
      // 列表数据
      tableData: [],
      // 分页
      pages: {
        page: 1,
        pageSize: 10
      },
      total: 0
    }
  },
  // 计算属性
  computed: {},
  watch: {},
  mounted () {
    this.searchLearn()
  },
  methods: {
    async searchLearn (clearPage = false) {
      if (clearPage) {
        this.pages.page = 1
        this.pages.pageSize = 10
      }
      const params = {
        ...this.pages,
        ...this.searchParams
      }
      const { total, list } = await eLearnkPage(params)
      this.total = total
      this.tableData = list
    },
    // 重置
    resetLearn () {
      this.searchParams.name = ''
      this.searchLearn(true)
    },
    // 新增
    addLearn () {
      this.$refs.learnInfo.isShow()
    },
    // 修改
    handleEdit (e, row) {
      this.$refs.learnInfo.isShow(row, true)
    },
    // 删除
    handleDelete (e, row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await deleteELearn({ id: row.id })
        this.searchLearn()
      }).catch(() => {
      })
    },
    // 条数
    handleSizeChange (val) {
      this.pages.pageSize = val
      this.searchLearn()
    },
    // 页数
    handleCurrentChange (val) {
      this.pages.page = val
      this.searchLearn()
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
